<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-project-add">
        <input name="deptId"  type="hidden" id="treeId"/>
			<div class="form-group">	
				<label class="col-sm-3 control-label">项目名称：</label>
				<div class="col-sm-8">
					<input id="projectName" name="projectName" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">归属部门：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="deptName" onclick="selectDeptTree()" readonly="true" id="treeName">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">项目标识：</label>
				<div class="col-sm-8">
					<input id="projectSign" name="projectSign" class="form-control" type="text">
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "system/project"
		$("#form-project-add").validate({
			rules:{
				projectName:{
					required:true,
					minlength: 1,
        			maxlength: 50,
                    remote: {
                        url: ctx + "system/project/checkProjectNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            name: function () {
                                return $.common.trim($("#projectName").val());
                            }
                        },
                        dataFilter: function (data, type) {
                        	return $.validate.unique(data);
                        }
                    }
				},
				deptName:{
					required:true
				},
				projectSign:{
					required:true,
					minlength: 1,
        			maxlength: 10,
                    remote: {
                        url: ctx + "system/project/checkProjectSignUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            name: function () {
                                return $.common.trim($("#projectSign").val());
                            }
                        },
                        dataFilter: function (data, type) {
                        	return $.validate.unique(data);
                        }
                    }
				}
			},
	    	messages: {
	            "projectName": {
	                remote: "项目名称已经存在"
	            },
	    		"projectSign": {
	                remote: "项目标识已经存在"
	            }
	        }
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	            $.operate.save(prefix + "/add", $('#form-project-add').serialize());
	        }
	    }
		
        /*项目管理-新增-选择部门树*/
        function selectDeptTree() {
        	var treeId = $("#treeId").val();
        	var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        	var url = ctx + "system/dept/selectDeptTree/" + deptId;
			var options = {
				title: '选择部门',
				width: "380",
				url: ctx + "system/dept/selectDeptTree/" + deptId,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
        
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
				var body = layer.getChildFrame('body', index);
    			$("#treeId").val(body.find('#treeId').val());
    			$("#treeName").val(body.find('#treeName').val());
    			layer.close(index);
			}
		}
	</script>
</body>
</html>
